//
//  ZFBFriendController.m
//  支付宝
//
//  Created by HaoYoson on 16/6/27.
//  Copyright © 2016年 HaoYoson. All rights reserved.
//

#import "NSArray+Addition.h"
#import "ZFBChatController.h"
#import "ZFBFriendCell.h"
#import "ZFBFriendController.h"

static NSString *cellid = @"friend_cell";
static NSString *friendCirclecellid = @"friendCircle_cell";

@interface ZFBFriendController ()

@property (strong, nonatomic) NSArray *friendsData;

@end

@implementation ZFBFriendController

- (instancetype)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.title = @"朋友";

    // 解析数据
    self.friendsData = [NSArray objectListWithPlistName:@"Friends.plist" clsName:@"ZFBFriend"];

    // 获取cell的nib
    UINib *nib = [UINib nibWithNibName:@"ZFBFriendCell" bundle:nil];
    // 注册单元格
    [self.tableView registerNib:nib forCellReuseIdentifier:cellid];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:friendCirclecellid];

    // 设置行高
    self.tableView.rowHeight = [[nib instantiateWithOwner:nil options:nil][0] bounds].size.height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section ? self.friendsData.count : 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:friendCirclecellid];
        cell.imageView.image = [UIImage imageNamed:@"APC_timeLineIcon"];
        cell.textLabel.text = @"生活圈";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }

    ZFBFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    cell.friends = self.friendsData[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == 0) {
        // 获取生活圈控制器
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"ZFBMoment" bundle:nil];
        UIViewController *vc = sb.instantiateInitialViewController;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 2) {    // 聊天
        ZFBChatController *vc = [[ZFBChatController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section ? 0 : 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

@end
